package cn.xm.lingshi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import cn.xm.lingshi.utlis.AlipayUtil;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;

public class ReturnServlet extends HttpServlet {
	final static Logger log = Logger.getLogger(ReturnServlet.class);
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		log.info("支付成功, 进入同步通知接口...");

		// 获取支付宝GET过来反馈信息
		Map<String, String> params = new HashMap<String, String>();
		Map<String, String[]> requestParams = request.getParameterMap();
		for (Iterator<String> iter = requestParams.keySet().iterator(); iter
				.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i]
						: valueStr + values[i] + ",";
			}
			// 乱码解决，这段代码在出现乱码时使用
			valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
			params.put(name, valueStr);
		}
		boolean signVerified = false;
		try {
			// 调用SDK验证签名
			signVerified = AlipaySignature.rsaCheckV1(params,
					AlipayUtil.alipayPublicKey, AlipayUtil.charSet,
					AlipayUtil.signType);
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		// ——请在这里编写您的程序（以下代码仅作参考）——
		if (signVerified) {
			// 商户订单号
			String out_trade_no = new String(request.getParameter(
					"out_trade_no").getBytes("ISO-8859-1"), "UTF-8");

			// 支付宝交易号
			String trade_no = new String(request.getParameter("trade_no")
					.getBytes("ISO-8859-1"), "UTF-8");
			/*// 交易状态
			String trade_status = new String(request.getParameter(
					"trade_status").getBytes("ISO-8859-1"), "UTF-8");*/

			// 付款金额
			String total_amount = new String(request.getParameter(
					"total_amount").getBytes("ISO-8859-1"), "UTF-8");

			// 修改叮当状态，改为 支付成功，已付款; 同时新增支付流水

			log.info("********************** 支付成功(支付宝同步通知) **********************");
			log.info("* 订单号: {" + out_trade_no + "}");
			log.info("* 支付宝交易号: {" + trade_no + "}");
			log.info("* 实付金额: {" + total_amount + "}");
			log.info("***************************************************************");
			HttpSession session=request.getSession();
			session.setAttribute("out_trade_no", out_trade_no);
			session.setAttribute("trade_no", trade_no);
			session.setAttribute("total_amount", total_amount);
			response.sendRedirect("pt_houtai.jsp");

		} else {
			log.info("支付, 验签失败...");
		}

		out.flush();
		out.close();
	}

}
